package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.sup.entity.SupShipMentMaterial;
import com.mrd.gtimp.sup.service.SupShipMentMaterialBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/sup")
public class SupShipMentMaterialAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SupShipMentMaterialBiz supShipMentMaterialBiz;

    /**
     * public String show() {
     * try {
     * page = initPage(SupShipMentMaterialBiz.findEntityCount(filters));
     * Order order = new Order();
     * order.add(sidx, sord);
     * SupShipMentMaterials = SupShipMentMaterialBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     * return SUCCESS;
     * }
     **/

    /**
     * 默认访问入口
     */
    @RequestMapping("/supShipMentMaterial-main")
    public String main() {
        return "/gtimp/sup/supShipMentMaterialList";
    }

    /**
     * 默认新增修改入口
     */
    @RequestMapping("/supShipMentMaterial-manage")
    public String manage(String id, HashMap map) {
        map.put("id", id);
        return "/gtimp/sup/supShipMentMaterialManage";
    }

    /**
     * 新增new入口
     */
    @RequestMapping("/supShipMentMaterial-save")
    public String save() {
        return "/gtimp/sup/supShipMentMaterialSave";
    }

    /**
     * 新增或修改信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SHIPMENTMATERIAL_SAVE)
    @RequestMapping("/supShipMentMaterial-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(SupShipMentMaterial supShipMentMaterial) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SHIPMENTMATERIAL_SAVE)) {
        try {
            if (StringUtils.isEmpty(supShipMentMaterial.getID())) {  // 新增操作
                // supShipMentMaterial.setCOM_ID(user.getCompany().getId());
                Company owner = new Company();
                owner.setId(LoginUtil.getLoginUser().getCompany().getId());
                supShipMentMaterial.setOwner(owner);
                supShipMentMaterial.setCREATOR(LoginUtil.getLoginUser().getAccount());
                supShipMentMaterial.setCTIME(new Date());
                supShipMentMaterial.setID(null);
            } else {  // 修改操作
                //// TODO: 2016/8/25 下拉框
                supShipMentMaterial.setMODIFIER(LoginUtil.getLoginUser().getAccount());
                supShipMentMaterial.setMTIME(new Date());
            }
            supShipMentMaterialBiz.saveOrupdateEntity(supShipMentMaterial);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supShipMentMaterial-find")
    @ResponseBody
    public SupShipMentMaterial find(SupShipMentMaterial supShipMentMaterial) {
        if (supShipMentMaterial != null && StringUtils.isNotEmpty(supShipMentMaterial.getID())) {
            try {
                supShipMentMaterial = supShipMentMaterialBiz.findEntityById(supShipMentMaterial
                        .getID());
                return supShipMentMaterial;
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 删除信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SHIPMENTMATERIAL_DEL)
    @RequestMapping("/supShipMentMaterial-del")
    @ResponseBody
    public Result del() {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SHIPMENTMATERIAL_DEL)) {
        try {
            String ids = HttpUtils.getRequest().getParameter("id");
            String[] idArr = ids.split(",");
            List<SupShipMentMaterial> desup = new ArrayList<>();
            for (String string : idArr) {
                SupShipMentMaterial shipMentMaterial = new SupShipMentMaterial();
                shipMentMaterial.setID(string);
                desup.add(shipMentMaterial);
            }
            supShipMentMaterialBiz.delete(desup);
            msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.DEL_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 查询
     */
    @RequestMapping("/supShipMentMaterial-findList")
    @ResponseBody
    public Page<SupShipMentMaterial> findList(Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from user
        try {
            SearchRule rule = new SearchRule("owner.id", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
//            page = initPage(supShipMentMaterialBiz.findEntityCount(filters));
//            Order order = new Order();
//            if (null != sidx && !"".equals(sidx)) {
//                order.add(sidx, sord);
//            }
//            supShipMentMaterials = supShipMentMaterialBiz.findEntityByFilter(page
//                    .getCurrentPageStartNo(), page.getPageSize(), filters, order);
            Page<SupShipMentMaterial> supShipMentMaterials = supShipMentMaterialBiz.findByJqgrid(jqgrid);
            return supShipMentMaterials;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Resource
    public void setSupShipMentMaterialBiz(SupShipMentMaterialBiz supShipMentMaterialBiz) {
        this.supShipMentMaterialBiz = supShipMentMaterialBiz;
    }
}
